import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import {useFileListStore} from "@/stores/fileListStore.js";
import {
    Back, Box, ChatLineRound, Check, CopyDocument, Delete,
    DocumentCopy, Download, Edit, Files, Finished, InfoFilled, Minus,
    MoreFilled, Plus, Pointer, Refresh, Right, Upload
} from "@element-plus/icons-vue";

import * as fileApi from  "@/api/fileApi.js"
import {usePathNavigationStore} from "@/stores/pathNavigation.js";
import left from "@/assets/left.png"
import button from "@/assets/button.png"
import top from "@/assets/top.png"
import {usePreviewImgStore} from "@/stores/previewImgStore.js";
import {ElMessage, ElNotification} from "element-plus";
import {usePreviewVideoStore} from "@/stores/previewVideoStore.js";
import {usePreviewAudioStore} from "@/stores/previewAudioStore.js";
import {useUploadStore} from "@/stores/upload.js";
import {usePreviewZipStore} from "@/stores/previewZipStore.js";
import {audioTypes, imageTypes, textTypes, videoTypes, zipTypes} from "@/utils/viewType.js";
import {useKeyStore} from "@/stores/keyStore.js";
import {usePreviewTextStore} from "@/stores/previewTextStore.js";

export const useRightMenuStore = defineStore('rightMenu', () => {
    const menuList = ref([
        { label: '打开', key: '', img: [left, left], split: true, disabled: false, icon: Check },
        { label: '下载', key: 'ctrl+s', img: [], split: false, disabled: false, icon: Download },
        { label: '压缩下载', key: 'ctrl+alt+s', img: [], split: false, disabled: false, icon: Box },
        { label: '上传', key: 'ctrl+e', img: [], split: true, disabled: false, icon: Upload },
        { label: '全选', key: 'ctrl+a', img: [], split: false, disabled: false, icon: MoreFilled },
        { label: '选择', key: 'ctrl+', img: [left], split: false, disabled: false, icon: Plus },
        { label: '抛弃', key: 'shift+', img: [left], split: false, disabled: false, icon: Minus },
        { label: '反选', key: 'ctrl+q', img: [], split: true, disabled: false, icon: Refresh },
        // { label: '复制', key: 'ctrl+c', img: [], split: false, disabled: true, icon: DocumentCopy },
        // { label: '剪切', key: 'ctrl+x', img: [], split: false, disabled: true, icon: CopyDocument },
        // { label: '粘贴', key: 'ctrl+v', img: [], split: true, disabled: true, icon: Files },
        // { label: '重命名', key: 'f2', img: [], split: false, disabled: true, icon: Edit },
        // { label: '删除', key: 'delete', img: [], split: true, disabled: true, icon: Delete },
        { label: '前进', key: '', img: [top], split: false, disabled: false, icon: Right },
        { label: '后退', key: '', img: [button], split: true, disabled: false, icon: Back },
        { label: '属性', key: 'ctrl+r', img: [], split: false, disabled: false, icon: ChatLineRound }
    ])
    const showMenu = ref(false)
    const menuPosition = ref({
        x: 0,
        y: 0
    })
    const receiveDownFile = ref(null)
    function updateReceiveDownFile(element) {
        if (receiveDownFile.value && !useFileListStore().selectedFiles.has(receiveDownFile.value)) {
            receiveDownFile.value.style.backgroundColor = useFileListStore().defaultBG
        }
        if (element.style.backgroundColor === useFileListStore().defaultBG){
            element.style.backgroundColor = useFileListStore().mouseIntoBG
        }
        receiveDownFile.value = element
    }

    function download() {
        let fileList = Array.from(useFileListStore().selectedFiles).map(item => JSON.parse(item.getAttribute("data-obj")));
        let absolutePathList = fileList.map(item => item.absolutePath)
        if(absolutePathList.length === 0) return
        if (fileList.some(item => item.directory) || fileList.length > 1) {
            fileApi.downloadZip(absolutePathList)
        }else {
            fileApi.download(absolutePathList[0])
        }
    }

    /**
     * 选择文件
     */
    function selectFile() {
        if (receiveDownFile.value) {
            useFileListStore().addFile(receiveDownFile.value)
        }
    }

    function discardFile() {
        if (receiveDownFile.value) {
            useFileListStore().delFile(receiveDownFile.value)
        }
    }

    function getRightFileData() {
        if(!receiveDownFile.value) return null
        return JSON.parse(receiveDownFile.value.getAttribute("data-obj"))
    }

    async function openDirectory(absolutePath,record) {
        if(absolutePath === null || absolutePath === undefined) absolutePath = getRightFileData().absolutePath
        await useFileListStore().apiList(absolutePath,null)
        useFileListStore().curPath = absolutePath;
        receiveDownFile.value = null
        useFileListStore().clearSelect()
        if(record) usePathNavigationStore().recordNewAction(absolutePath)
    }

    function parallelDownloadZip() {
        let fileList = Array.from(useFileListStore().selectedFiles).map(item => JSON.parse(item.getAttribute("data-obj")));
        let absolutePathList = fileList.map(item => item.absolutePath)
        if(absolutePathList.length === 0) return
        fileApi.parallelDownloadZip(absolutePathList)
    }

    async function openFile() {
        let fileEntry = getRightFileData();
        if(fileEntry.directory) {
            await openDirectory(null,true)
            return
        }
        // const imageTypes = new Set(['jpg', 'jpeg', 'png']);
        // const videoTypes = new Set(['mp4']);
        // const audioTypes = new Set(['mp3','flac']);
        // const zipTypes = new Set(['zip']);
        let store = null;
        if (imageTypes.has(fileEntry.suffixName)) store = usePreviewImgStore()
        if (videoTypes.has(fileEntry.suffixName)) store = usePreviewVideoStore()
        if (audioTypes.has(fileEntry.suffixName)) store = usePreviewAudioStore()
        if (zipTypes.has(fileEntry.suffixName)) store = usePreviewZipStore()
        if (textTypes.has(fileEntry.suffixName)) store = usePreviewTextStore()

        if(store == null && useKeyStore().keyDown(['Alt'])) store = usePreviewTextStore()
        if(store) {
            store.title = fileEntry.name
            store.absolutePath = fileEntry.absolutePath

            // 部分预览需要初始化一下
            try {
                store.init()
            } catch (e) {
            }

            store.show = true

            return
        }

        ElMessage.error(`不支持预览 ${fileEntry.suffixName} 文件`)
        ElNotification({
            title: '提示',
            message: "按住 'Alt' 键以文本形式打开",
            duration: 3000,
        })
    }
    function upload() {
        useUploadStore().show = true
    }
    return { menuList,showMenu,menuPosition,updateReceiveDownFile,selectFile,download,receiveDownFile,getRightFileData,openDirectory,parallelDownloadZip, discardFile,openFile,upload }
})
